package com.live.dengdengtv.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.gson.CustomMsg;
import com.live.dengdengtv.utils.DateUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.TIMMessage;
import com.tencent.TIMTextElem;

import java.util.Date;
import java.util.List;

/**
 * Created by wyf on 2016/9/22.
 */
public class LinkMicDialogAdpter extends BaseAdapter {

    private Context context;
    private List<CustomMsg.CustomUserInfo> dataList;
    private LinkMicListener mLinkMicListener;
    public LinkMicDialogAdpter(Context context, List<CustomMsg.CustomUserInfo> data, LinkMicListener linkMicListener) {
        this.context = context;
        this.dataList = data;
        this.mLinkMicListener = linkMicListener;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView != null) {
            holder = (ViewHolder)convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.item_link_mic, null);

            holder = new ViewHolder();
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
            holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
            holder.head_iv = (ImageView) convertView.findViewById(R.id.head_iv);
            holder.sex_iv = (ImageView) convertView.findViewById(R.id.sex_iv);
            holder.level_iv = (ImageView) convertView.findViewById(R.id.level_iv);


            holder.link_mic_bt = (Button) convertView.findViewById(R.id.link_mic_bt);

            convertView.setTag(holder);
        }

        final CustomMsg.CustomUserInfo memberInfo = dataList.get(position);
        String nickName = memberInfo.nickName;
        String faceUrlc = memberInfo.faceUrl;

        holder.name_tv.setText(TextUtils.isEmpty(nickName)? memberInfo.userId:nickName);
        holder.msg_tv.setText(memberInfo.selfSign);

        if(!TextUtils.isEmpty(faceUrlc)){
            ImageLoader.getInstance().displayImage(faceUrlc, holder.head_iv);
        }

        if(memberInfo.isOnVideoChat){
            holder.link_mic_bt.setText("断开");
        }else {
            holder.link_mic_bt.setText("连麦");
        }
        holder.link_mic_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLinkMicListener.onPreLinkMic(memberInfo);
            }
        });

        return convertView;
    }

    public static interface LinkMicListener{
        void onPreLinkMic(CustomMsg.CustomUserInfo memberInfo);
    }
    private static class ViewHolder{
        TextView name_tv;
        TextView msg_tv;
        TextView time_tv;
        ImageView head_iv;
        ImageView sex_iv;
        ImageView level_iv;

        Button link_mic_bt;
    }
}
